// 引入包并实例化
const express = require('express');
const router = express.Router();
// 导入自定义mysql模块
const db = require('../db/sqlDB');
// 导入集中管理的sql语句
const $sql = require('../db/sqlMap')
// api根路由测试
router.get('/',(req,res)=>{
  res.json({
      msg:'hello api'
  })
})
router.get('/userinfo', (req, res) => {
    res.send({
      status: 0,
      message: 'GET 请求成功！',
      data: {
        name: 'zhangsan',
        age: 18,
        gender: '男'
      }
    })
  })
   
router.post('/aboutme', (req, res) => {
    res.send({
      status: 0,
      message: 'POST 请求成功！',
      data: {
        myname: 'aFlowerDemon',
        myage: 21,
        mygender: '男'
      }
    })
  })

router.get('/login',(req,res)=>{
    console.log(req.query)
    let {us,ps} = req.query
    //处理数据
    if(us=='admin'&&ps==123){
        res.send({err:0,msg:'login ok'})
    }else{
        res.send({err:-1,msg:'user pwd err'})
    }
})

router.post('/reg',(req,res)=>{
    console.log(req.body)

    let {us,ps} = req.body
    if(us=='admin'&&ps==123){
        res.send({err:0,msg:'login ok'})
    }else{
        res.send({err:-1,msg:'user pwd err'})
    }
})
// 查找所有数据
router.get('/test1',(req,res)=>{
    db.query($sql.findAll, (err,results)=>{
        res.send({
          code:0,
          data:results  
        })
      })
})
// 查找一条数据
router.get('/test2',(req,res)=>{
    db.query($sql.findOne,req.query.id, (err,results)=>{
        res.send({
          code:0,
          data:results  
        })
      })
})
  module.exports = router